﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DuelQuizz.contract;
using DuelQuizzServer;

namespace DuelQuizz.Views
{
    /// <summary>
    /// Logique d'interaction pour MainPage.xaml
    /// </summary>
    public partial class MainPage : UserControl
    {
        public static MainPage window;

        public User user;
        private List<Party> list_party;

        public MainPage(User user)
        {
            window = this;
            InitializeComponent();
            this.user = user;
            this.user.friends = AppServices.getDuelQuizzServices().getFriends().ToList();
            
            /* Load coponnent */
            label_pseudo.Content =user.username;
            label_rank.Content = user.score;

            list_party = AppServices.getDuelQuizzServices().getParties().ToList();
          //  Listview_liste_room.ItemsSource = list_party;

            Amis.ItemsSource = user.friends;

            refreshNotification();
        }

        public static void refreshNotification()
        {
            if (window != null)
            {
                window.user.notifications = AppServices.getDuelQuizzServices().getNotifications();
                window.updateNbUnreadNotification(window.user.getNbUnreadNotification());
            }
        }

        public void updateNbUnreadNotification(int nb){
            button_notification.Content = nb + " notifications";
        }

        public static void refreshPartyList()
        {
            window.list_party = AppServices.getDuelQuizzServices().getParties().ToList();
           // window.Listview_liste_room.ItemsSource = window.list_party;
        }

        #region Click Events
            private void Button_Click_Notifications(object sender, RoutedEventArgs e)
            {
                MainWindow.changeWindows(new NotificationsPage(this));
            }

            private void Button_Click_Creer_Room(object sender, RoutedEventArgs e)
            {
                AppServices.getDuelQuizzServices().createParty("");
            }

            private void Button_Click_Duel_Aleatoire(object sender, RoutedEventArgs e)
            {

            }

            private void Button_Click_Duel_Ami(object sender, RoutedEventArgs e)
            {

            }

            private void Button_Click_Ajouter_Ami(object sender, RoutedEventArgs e)
            {
                if (friend_pseudo_to_add.Text == "") {
                    MessageBox.Show("Veuillez entrer le pseudo de votre ami");
                    return;
                }

                User friend = AppServices.getDuelQuizzServices().getUserWithUsername(friend_pseudo_to_add.Text);
                if(friend == null){
                    MessageBox.Show("Joueur introuvable :( ");
                    return;
                }

                if (AppServices.getDuelQuizzServices().addFriend(friend.id)){
                    user.friends.Add(friend);
                    friend_pseudo_to_add.Text = "";
                    refreshFriendList();
                    AppServices.getDuelQuizzServices().creerNotification(friend.id, "friend_request"); 
                    MessageBox.Show("Ami ajouté !");
                }
                else {
                    MessageBox.Show("Echec ajout ami"); 
                }

            }

            private void refreshFriendList()
            {
                Amis.ItemsSource = null;
                Amis.ItemsSource = user.friends;
            }

            private void Button_Click_Voir_Classement(object sender, RoutedEventArgs e)
            {
                LeaderboardPage l = new LeaderboardPage(this);
                MainWindow.changeWindows(l);
            }

            private void Button_Click_Historique_Parties(object sender, RoutedEventArgs e)
            {

            }

            private void Button_Click_Rejoindre_Partie(object sender, RoutedEventArgs e)
            {

            }
        #endregion

            private void Button_Click_AddQuestion(object sender, RoutedEventArgs e)
            {
                MainWindow.changeWindows(new AddQuestion(this));

            }

            private void btnTrain_Click(object sender, RoutedEventArgs e)
            {
                MainWindow.changeWindows(new Categories(user));
            }
    }
}
